package com.yanqu.road.dao.impl.monopolymarket;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketReportData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/18 17:11
 */
public class MonopolyMarketReportDataDaoImpl extends TempDataObjectDao<MonopolyMarketReportData> {

    public static String getMonopolyMarketReportDataSql = "select * from `t_u_monopolymarket_report_data` where `userId`=? and `type`=? and `time` > ? ORDER BY `time` DESC limit ?";
    public List<MonopolyMarketReportData> getMonopolyMarketReportDataList(long userId, int type,int startTime, int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        List list = new ArrayList();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketReportDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, type);
            call.setInt(i++, startTime);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateMonopolyMarketReportDataSql = "update `t_u_monopolymarket_report_data` set type=?,posIndex=?,robId=?,isWin=?,time=?,robInfo=? where userId=?";
    @Override
    protected boolean updateData(MonopolyMarketReportData monopolyMarketReportData) {
        monopolyMarketReportData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketReportData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketReportData.getPosIndex()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketReportData.getRobId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketReportData.getIsWin()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketReportData.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketReportData.getRobInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketReportData.getUserId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketReportDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketReportDataSql = "insert into `t_u_monopolymarket_report_data`(userId,type,posIndex,robId,isWin,time,robInfo) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketReportData monopolyMarketReportData) {
        monopolyMarketReportData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketReportData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketReportData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketReportData.getPosIndex()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketReportData.getRobId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketReportData.getIsWin()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketReportData.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketReportData.getRobInfo())));
        if (getDb().executeNoneQuery(insertMonopolyMarketReportDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketReportData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketReportData monopolyMarketReportData = new MonopolyMarketReportData();
        monopolyMarketReportData.setUserId(rs.getLong("userId"));
        monopolyMarketReportData.setType(rs.getInt("type"));
        monopolyMarketReportData.setPosIndex(rs.getInt("posIndex"));
        monopolyMarketReportData.setRobId(rs.getLong("robId"));
        monopolyMarketReportData.setIsWin(rs.getBoolean("isWin"));
        monopolyMarketReportData.setTime(rs.getInt("time"));
        monopolyMarketReportData.setRobInfo(JSON.parseObject(rs.getString("robInfo"), UserBaseInfo.class));

        return monopolyMarketReportData;
    }
}
